<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item label="讲师名称">
        <el-input v-model="searchObj.name" placeholder="讲师名称"></el-input>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="searchObj.level" placeholder="讲师头衔">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
        <el-button type="primary" @click="getAll">全部</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="115">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="讲师名称" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="讲师资历" width="520"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.career }}</span>
        </template>
      </el-table-column>
      <el-table-column label="讲师头衔" width="220" align="center">
        <template slot-scope="scope">
          {{ scope.row.level==1?'高级讲师':'首席讲师' }}
        </template>
      </el-table-column>

      <el-table-column align="center" prop="created_at" label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="updateTeacher(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      align="center"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList" />

  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import teacher from '@/api/teacher'


  export default {
    components: { Pagination },
    data() {
      return {
        list: null,//分页集合
        total: 0,//总记录数
        listQuery: {//分页参数
          page: 1,
          limit: 10
        },
        searchObj: {//查询条件
          name: '',
          level: ''
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      //查询讲师列表
      getList(){
        teacher.getTeacherList(this.listQuery.page,this.listQuery.limit,this.searchObj)
          .then(response=>{
            this.list = response.data.list
            this.total = response.data.total

          })
      },
      //查询全部
      getAll(){
        this.searchObj={}
        this.listQuery.page = 1
        teacher.getTeacherList(this.listQuery.page,this.listQuery.limit,this.searchObj)
          .then(response=>{
            this.list = response.data.list
            this.total = response.data.total

          })
      },
      //删除
      remove(id){
        this.$confirm('此操作将永久删除该讲师, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          teacher.deleteTeacher(id)
            .then(response=>{
              if(response.data.del){
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                //重查数据
                this.getList()
              }
            })
        })
      },
      //进入编辑页面
      updateTeacher(id){
        this.$router.push({path:'/teacher/edit/'+id})
      }


    }
  }
</script>
